package com.define.ssh.action.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import com.define.ssh.model.Func;
import com.define.ssh.model.Role;
import com.define.ssh.model.RoleFunc;
import com.define.ssh.service.FuncService;
import com.define.ssh.service.RoleFuncService;
import com.define.ssh.service.RoleService;
import com.define.ssh.service.UserInfoService;

/**
 * 角色操作
 * 
 * @author hcy
 * 
 */
public class RoleAction extends BaseAdminAction {
	private static final long serialVersionUID = 5L;

	/** 页面传递参数过来 */
	protected int pageSize;
	protected int pageNum;

	@Resource(name = "roleServiceImpl")
	private RoleService roleService;
	@Resource(name = "userInfoServiceImpl")
	private UserInfoService userInfoService;
	@Resource(name = "funcServiceImpl")
	private FuncService funcService;
	@Resource(name = "roleFuncServiceImpl")
	private RoleFuncService roleFuncService;
	private Role role;

	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() {
		pager = roleService.findPager(pager);
		return LIST;
	}

	// 删除
	public String delete() {
		final StringBuffer logInfoStringBuffer = new StringBuffer("删除角色: ");
		for (final Long id : ids) {
			final Role ss = roleService.load(id);
			// 该角色对应的用户
			userInfoService.delUserInfoByRole(id);
			// 删除角色
			roleService.delete(ss);
		}
		logInfo = logInfoStringBuffer.toString();
		return ajax(Status.success, "删除成功!");
	}

	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		role = roleService.load(id);
		return INPUT;
	}

	// 保存
	public String save() {
		role.setCreateDate(new Date());
		roleService.save(role);
		logInfo = "添加角色: " + role.getName();
		return ajax(Status.success, "添加角色成功");
	}

	// 更新
	public String update() {
		final Role role1 = roleService.load(id);
		role1.setName(role.getName());
		role1.setIsvalid(role.getIsvalid());
		role1.setRemark(role.getRemark());
		role1.setModifyDate(new Date());
		roleService.update(role1);
		logInfo = "编辑角色: " + role.getName();
		return ajax(Status.success, "修改角色成功");
	}

	// 权限分配
	public String qxfp() {
		pager = roleService.findPager(pager);
		return "qxfp";
	}

	// 转到分配权限页面
	public String to_qxfp() {
		role = roleService.load(id);
		// 所有权限
		final HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("isvalid", "0");
		hashMap.put("levels", "0");
		final List<Func> parentListAll = funcService
				.getAllListByParameter(hashMap);// 获取父类功能集合
		hashMap.put("levels", "1");
		final List<Func> childListAll = funcService
				.getAllListByParameter(hashMap);// 获取非父类功能集合
		// 已有的权限
		final List<Func> checkFuncList = funcService.getFuncByRoleId(role
				.getId());
		super.getRequest().setAttribute("funcListAll", parentListAll);
		super.getRequest().setAttribute("childListAll", childListAll);
		super.getRequest().setAttribute("checkFuncList", checkFuncList);
		return "qxfp_input";
	}

	// 分配权限
	public String save_qxfp() {
		role = roleService.load(id);
		// 删除原来的权限
		roleFuncService.delRoleFuncByRoleId(role.getId());
		if (ids != null && !"".equals(ids)) {
			for (final Long id : ids) {
				final Func fc = funcService.get(id);
				final RoleFunc rf = new RoleFunc();
				rf.setFunc(fc);
				rf.setRole(role);
				roleFuncService.save(rf);
			}
		}
		return ajax(Status.success, "权限分配成功,对应的角色用户需要重新登录生效!");
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}
}
